package com.srs.dao.cfg;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.cfg.Role;
import com.srs.util.HibernateUtil;

public class RoleDao {

	final static Logger logger = LoggerFactory.getLogger(RoleDao.class);

	private List<Role> roleList;
	private Role role;

	public void delete(Integer roleId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			role = (Role) session.get(Role.class, roleId);
			session.delete(role);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Role> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			roleList = session.createQuery("from Role").list();
			return roleList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<Role> list(String rolesInGroup) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			if (rolesInGroup!=null && !rolesInGroup.equalsIgnoreCase("")){
				roleList = session.createQuery("from Role r where r.roleId not in (" + rolesInGroup + ")").list();
			} else {
				roleList = session.createQuery("from Role").list();
			}
			return roleList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}
	
	public Role get(Integer roleId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from Role r where r.roleId=:roleId");
			q.setInteger("roleId", roleId);
			role = (Role) q.uniqueResult();
			if (role==null){
				return new Role();
			}
			return role;
		} finally {
			session.close();
		}

	}

	public Boolean nameIsUsed(String roleName) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from Role g where g.roleName=:roleName");
			q.setString("roleName", roleName);
			roleList = q.list();
			if (roleList.size()==0) {
				return false;
			}
			return true;
		} finally {
			session.close();
		}

	}		
	
	public void insert(Role role) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(role);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(Role role) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(role);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

}
